﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsApplication1
{
    public class DecisionEngine
    {
        public Action DecisionAction { get; set; }
        public DecisionEngine(Player player, int dealerUpCard, Decision type)
        {
            getDecisionAction(player, dealerUpCard, type);
        }

        private void getDecisionAction(Player player, int dealerUpCard, Decision type)
        {
            //statements to build decision process
            switch (type)
            {
                case Decision.Ace:
                    DecisionAction = getAceDecision(player, dealerUpCard);
                    break;
                case Decision.Standard:
                    DecisionAction = getStandardDecision(player, dealerUpCard);
                    break;
                case Decision.split:
                    DecisionAction = getsplitDecision(player, dealerUpCard);
                    break;
            }

        }

        private Action getsplitDecision(Player player, int dealerUpCard)
        {
            Action Result;//  initialization

            if (player.Total == 4 || player.Total == 6)//Player has 2, 2
            {
                Result = ((dealerUpCard > 1) && (dealerUpCard < 8)) ? Action.Split : Action.Hit;//isSplitOrHit
            }
             
            else if (player.Total == 8) //Player has 4, 4
            {
                Result = ((dealerUpCard == 5) || (dealerUpCard == 6)) ? Action.Split : Action.Hit;//isSplitOrHit
            }
            else if (player.Total == 12) //Player has 6, 6 : If the player has 5, 5 we treat it is a hard, non-splittable 10.
            {
                Result = ((dealerUpCard > 1) && (dealerUpCard < 7)) ? Action.Split : Action.Hit;//isSplitOrHit
            }
            else if (player.Total == 14) //Player has 7, 7
            {
                Result = ((dealerUpCard > 1) && (dealerUpCard < 8)) ? Action.Split : Action.Hit;  //isSplitOrHit
            }
            else if (player.Total == 16)//Player has 8, 8
            {
                Result = Action.Split;
            }
            else if (player.Total == 18) //Player has 9, 9
            {
                Result = ((dealerUpCard == 1) || (dealerUpCard == 7) || (dealerUpCard == 10)) ? Action.Stand : Action.Split;
            }
            else if (player.Total == 20) //Player has 10, 10 or J, J or Q, Q or K, K
            {
                Result = Action.Stand;
            }
            else if (player.Total == 22) //Player has A, A
            {
                Result = Action.Split;
            }
            else
            {
                Result = Action.Split;
            }

            return Result;
        }

        private Action getStandardDecision(Player player, int dealerUpCard)
        {
            Action Result; 

            if (player.Total < 9)
            {
                Result = Action.Hit;
            }
            else if (player.Total == 9)
            {
                Result = (((dealerUpCard > 2) && (dealerUpCard < 7)) && (player.numberOfCards == 2)) ? Action.Double : Action.Hit;
            }
            else if (player.Total == 10)
            {
                Result = (((dealerUpCard > 1) && (dealerUpCard < 10)) && (player.numberOfCards == 2))? Action.Double:Action.Hit;
            }
            else if (player.Total == 11)
            {
                Result =  ((dealerUpCard > 1) && (player.numberOfCards == 2))?Action.Double:Action.Hit;
            }
            else if (player.Total == 12)
            {
                 Result = ((dealerUpCard == 4) || (dealerUpCard == 5) || (dealerUpCard == 6))?Action.Stand:Action.Hit;
            }
            else if ((player.Total >= 13) && (player.Total <= 16))
            {
                    Result = ((dealerUpCard > 1) && (dealerUpCard < 7)) ? Action.Stand : Action.Hit;
            }
            else if (player.Total >= 17)
            {
                Result = Action.Stand;
            }
            else
            {
                Result = Action.Stand;
            }

            return Result;
        }

        private Action getAceDecision(Player player, int dealerUpCard)
        {
            Action Result;
            List<int> DealerUpCards;

            if (player.Total == 13) //Player has A, 2
            {
                DealerUpCards = new List<int> { 5, 6 };
                Result = (DealerUpCards.Contains(dealerUpCard) && (player.numberOfCards == 2)) ? Action.Double : Action.Hit;
            }
            else if (player.Total == 14) //Player has A, 3
            {
                DealerUpCards = new List<int> { 5, 6 };
                Result = (DealerUpCards.Contains(dealerUpCard) && (player.numberOfCards == 2)) ? Action.Double : Action.Hit;
            }
            else if (player.Total == 15) //Player has A, 4
            {
                DealerUpCards = new List<int> {4, 5, 6 };
                Result = (DealerUpCards.Contains(dealerUpCard) && (player.numberOfCards == 2)) ? Action.Double : Action.Hit;
            }
            else if (player.Total == 16) //Player has A, 5
            {
                DealerUpCards = new List<int> { 4, 5, 6 };
                Result = (DealerUpCards.Contains(dealerUpCard) && (player.numberOfCards == 2)) ? Action.Double : Action.Hit;
            }
            else if (player.Total == 17) //Player has A, 6
            {
                DealerUpCards = new List<int> { 3,4, 5, 6 };
                Result = (DealerUpCards.Contains(dealerUpCard) && (player.numberOfCards == 2)) ? Action.Double : Action.Hit;
             }
            else if (player.Total == 18) //Player has A, 7
            {
                DealerUpCards = new List<int> { 3, 4, 5, 6 };
                Result = (DealerUpCards.Contains(dealerUpCard) && (player.numberOfCards == 2)) ? Action.Double : Action.Hit;
            }
            else if (player.Total == 18) //Player has A, 7
            {
                DealerUpCards = new List<int> { 2, 7, 8 };
                Result = (DealerUpCards.Contains(dealerUpCard)) ? Action.Stand : Action.Hit;
            }
            else if (player.Total > 18)
            {
                Result = Action.Stand;
            }
            else
            {
                Result = Action.Stand;
            }
            return Result;
        }

    }
}
